/**
 * 
 */
package com.alstudio.core.message.system;

import java.util.ArrayList;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.core.message.ALMessageNotificationManager;
import com.alstudio.core.message.LoadMessageCallback;
import com.alstudio.module.xmpp.protocol.xmpp.data.ALXmppEventType;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALMessage;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.utils.asyncTask.ALAsycTask;
import com.alstudio.utils.asyncTask.ALAsyncTaskCallback;
import com.alstudio.utils.log.ALLog;

/**
 * 处理保存系统消息
 * 
 * @author alonso lee
 * 
 */
public class SystemMessageManager {

	private static SystemMessageManager mInstance;

	public static SystemMessageManager getSystemMessageManager() {
		if (mInstance == null) {
			mInstance = new SystemMessageManager();
		}
		return mInstance;
	}

	public void saveSystemMessage(ALMessage msg) {
		startSaveSystemMessage(msg);
	}

	private ALAsyncTaskCallback saveSsystemMessageCallback = new ALAsyncTaskCallback() {

		@Override
		public Object onTaskStart(Object obj) {
			// TODO Auto-generated method stub
			ALMessage msg = (ALMessage) obj;
			ALLog.d("开始保存系统消息 " + msg.getBody());
			ALLocalEnv.getLocalEnv().getDatabase().saveSystemMsg(msg);
			ALLog.d("保存完成,开始通知");
			ALMessageNotificationManager
					.getDefaultALMessageNotificationManager()
					.showMessageNotification(msg, true);
			// ALLocalEnv.getLocalEnv().getNotificationCenterManager()
			// .showALNotification();
			ALXmppEvent data = new ALXmppEvent(
					ALXmppEventType.XMPP_RECEIVE_NEW_SYSTEM_MESSAGE_EVENT);
			data.setMessage(msg);
			ALLocalEnv.mXmppEventNotifyManager.notifyAllListener(data);
			return msg;
		}

		@Override
		public Object onTaskFinish(Object obj) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Object onTaskCancel() {
			// TODO Auto-generated method stub
			return null;
		}
	};

	/**
	 * 开始保存系统消息
	 * 
	 * @param msg
	 */
	private void startSaveSystemMessage(ALMessage msg) {
		ALAsycTask saveSystemMessageTask = new ALAsycTask(
				saveSsystemMessageCallback);
		saveSystemMessageTask.execute(msg);
	}

	/**
	 * 读取系统消息
	 * 
	 * @author alonso lee
	 * 
	 */
	private class LoadSytemMessageTask implements ALAsyncTaskCallback {

		private LoadMessageCallback callback;

		/**
		 * 
		 */
		public LoadSytemMessageTask(LoadMessageCallback callback) {
			// TODO Auto-generated constructor stub
			this.callback = callback;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.alstudio.utils.asyncTask.ALAsyncTaskCallback#onTaskStart(java
		 * .lang.Object)
		 */
		@Override
		public Object onTaskStart(Object obj) {
			// TODO Auto-generated method stub
			int offset = (Integer) obj;
			ArrayList<ALMessage> msgs = new ArrayList<ALMessage>();
			msgs.addAll(ALLocalEnv.getLocalEnv().getDatabase()
					.getSystemMsgList(offset));

			return msgs;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.alstudio.utils.asyncTask.ALAsyncTaskCallback#onTaskFinish(java
		 * .lang.Object)
		 */
		@Override
		public Object onTaskFinish(Object obj) {
			// TODO Auto-generated method stub
			if (callback != null) {
				callback.onLoadCompleted(obj);
			}
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see com.alstudio.utils.asyncTask.ALAsyncTaskCallback#onTaskCancel()
		 */
		@Override
		public Object onTaskCancel() {
			// TODO Auto-generated method stub
			return null;
		}
	}

	/**
	 * 分页读取系统消息
	 * 
	 * @param offset
	 * @param callback
	 */
	public void loadSystemMessage(int offset, LoadMessageCallback callback) {

		ALAsycTask loadSystemMessageTask = new ALAsycTask(
				new LoadSytemMessageTask(callback));
		loadSystemMessageTask.execute(offset);
	}

	/**
	 * 读取未读系统消息数
	 * 
	 * @author alonso lee
	 * 
	 */
	private class LoadUnReadSystemMessageCountTask implements
			ALAsyncTaskCallback {

		private LoadMessageCallback callback;

		/**
		 * 
		 */
		public LoadUnReadSystemMessageCountTask(LoadMessageCallback callback) {
			// TODO Auto-generated constructor stub
			this.callback = callback;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.alstudio.utils.asyncTask.ALAsyncTaskCallback#onTaskStart(java
		 * .lang.Object)
		 */
		@Override
		public Object onTaskStart(Object obj) {
			// TODO Auto-generated method stub
			int count = ALLocalEnv.getLocalEnv().getDatabase()
					.getCountOfUnreadSystemMsg();
			return count;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.alstudio.utils.asyncTask.ALAsyncTaskCallback#onTaskFinish(java
		 * .lang.Object)
		 */
		@Override
		public Object onTaskFinish(Object obj) {
			// TODO Auto-generated method stub
			if (callback != null) {
				callback.onLoadCompleted(obj);
			}
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see com.alstudio.utils.asyncTask.ALAsyncTaskCallback#onTaskCancel()
		 */
		@Override
		public Object onTaskCancel() {
			// TODO Auto-generated method stub
			return null;
		}
	}

	/**
	 * 读取未读系统消息数目
	 * 
	 * @param offset
	 * @param callback
	 */
	public void loadUnReadSystemMessageCount(LoadMessageCallback callback) {

		ALAsycTask loadUnReadSystemMessageCountTask = new ALAsycTask(
				new LoadUnReadSystemMessageCountTask(callback));
		loadUnReadSystemMessageCountTask.execute("");
	}

	/************************************************************ 设为已读 ****************************************************************/

	/**
	 * 全部设为已读
	 */
	public void setAllMessageReaded() {
		new Thread() {
			public void run() {
				ALLocalEnv.getLocalEnv().getDatabase()
						.updateSystemMessageByType();
			};
		}.start();
	}

	/**
	 * 单条设为已读
	 * 
	 * @param msgId
	 */
	public void setMessageReadedById(final String msgId) {
		new Thread() {
			public void run() {
				ALLocalEnv.getLocalEnv().getDatabase()
						.setSystemMessageReaded(msgId);
			};
		}.start();
	}

	/**
	 * 删除单挑消息
	 * 
	 * @param msgId
	 */
	public void deleteMessageById(final String msgId) {
		new Thread() {
			public void run() {
				ALLocalEnv.getLocalEnv().getDatabase()
						.deleteSystemMsgById(msgId);
			};
		}.start();
	}

	/**
	 * 删除所有留言
	 */
	public void deleteAllMessage() {
		new Thread() {
			public void run() {
				ALLocalEnv.getLocalEnv().getDatabase().deleteAllSystemMsg();
			};
		}.start();
	}

}
